
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;

import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.scene.Node;
import com.db.DashBoardDao;
import com.to.CategoryTo;
import com.to.ResultTo;
 
public class TableViewSample extends Application {
 
    private TableView<ResultTo> table = new TableView<ResultTo>();
    
    final HBox hb = new HBox();
    final HBox hb2 = new HBox();
    final HBox hb3 = new HBox();
    final VBox vbox2 = new VBox();
    DashBoardDao dao = null;
    PieChart chart = null;
    public static void main(String[] args) {
        launch(args);
    }
 
    @SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(900);
        stage.setHeight(600);
 
        BorderPane border = new BorderPane();
        HBox hbox = addHBox();
        border.setTop(hbox);
        addStackPane(hbox);
        dao = new DashBoardDao();
        
        final Label prodCat = new Label("Product Category :");
        prodCat.setFont(new Font("Arial", 14));
        final ComboBox comb = getCatList();
        comb.setPrefWidth(150);
        comb.getSelectionModel().selectFirst(); 
        chart = new PieChart();
        chart.setTitle("Products in the Category");
        chart.setData(dao.getDataCatwise(comb.getValue().toString()));
        
       /* 
        final TableView<ResultTo> table = new TableView<ResultTo>();
    	table.setMaxHeight(350);
        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
            new PropertyValueFactory<ResultTo, String>("firstName"));
        
        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
            new PropertyValueFactory<ResultTo, Double>("lastName"));
        
        table.setItems(dao.getCatResult(comb.getValue().toString()));
        table.getColumns().addAll(firstNameCol, lastNameCol);*/
    	
        populateTable(comb.getValue().toString());
        
        
        
        
        //chart = getPieChart(((CategoryTo)comb.getSelectionModel().getSelectedItem()).getId());
        comb.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<CategoryTo>() {

			@Override
			public void changed(ObservableValue<? extends CategoryTo> arg0,CategoryTo arg1, CategoryTo arg2) {
				if (arg2 != null) {
                    System.out.println("Selected category : " + arg2.getDesc());
                    //chart = getPieChart(arg2.getDesc());
                    chart.setData(dao.getDataCatwise(arg2.getDesc()));
                    populateTable(arg2.getDesc());
                    System.out.println("Selected category : " + arg2.getDesc());
                 }
				
			}
        	
		});
        
        final Label caption = new Label("");
        caption.setTextFill(Color.DARKORANGE);
        caption.setStyle("-fx-font: 24 arial;");
        
        for (final PieChart.Data data : chart.getData()) {
            data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED,
                new EventHandler<MouseEvent>() {

					@Override
					public void handle(MouseEvent e) {
						caption.setTranslateX(e.getSceneX());
			            caption.setTranslateY(e.getSceneY());
			            caption.setText(String.valueOf(data.getPieValue()) + "%");
						
					}
                   
                });
        }
        
        
     
        
        hb3.setAlignment(Pos.CENTER);
        hb3.setPrefWidth(950);
        hb3.setSpacing(5);
        hb3.getChildren().addAll(prodCat,comb);
        //System.out.println("******************************************************"+((CategoryTo)comb.getSelectionModel().getSelectedItem()).getId());
        final Label label = new Label("Product List for the Category");
        label.setFont(new Font("Arial", 20));
 
        
        
        
        
        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);
        vbox.setAlignment(Pos.CENTER);
        hb2.setSpacing(10);
        hb2.setPadding(new Insets(10, 0, 0, 10));
        hb2.setAlignment(Pos.CENTER);
        hb2.getChildren().addAll(vbox,chart,caption);
        
        vbox2.setAlignment(Pos.CENTER);
        vbox2.setSpacing(5);
        vbox2.setPadding(new Insets(10, 0, 0, 10));
        vbox2.getChildren().addAll(hb3,hb2);
        border.setCenter(vbox2);
        ((Group) scene.getRoot()).getChildren().addAll(border);
 
        stage.setScene(scene);
        stage.show();
    }
    
    @SuppressWarnings("unchecked")
	private void populateTable(String cat) {
    	table.setMaxHeight(350);
        table.getItems().clear();
        dao = new DashBoardDao();
        table.setItems(dao.getCatResult(cat));
        TableColumn<ResultTo, String> contactIdCol = new TableColumn<ResultTo, String>("Product Name");
        contactIdCol.setMinWidth(150);
        contactIdCol.setCellValueFactory(new PropertyValueFactory<ResultTo, String>("desc"));
        TableColumn<ResultTo, Double> firstNameCol = new TableColumn<ResultTo, Double>("Total Sale ($)");
        firstNameCol.setMinWidth(150);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<ResultTo, Double>("amount"));
        
        table.getColumns().setAll(contactIdCol, firstNameCol);
   }
    
    
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
	private TableView<Person> getTable(){
    	
    	ObservableList<Person> data =
                FXCollections.observableArrayList(
                new Person("Jacob", "Smith", "jacob.smith@example.com"),
                new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
                new Person("Ethan", "Williams", "ethan.williams@example.com"),
                new Person("Emma", "Jones", "emma.jones@example.com"),
                new Person("Michael", "Brown", "michael.brown@example.com"));
    	
    	
    	TableView<Person> table = new TableView<Person>();
    	table.setMaxHeight(350);
        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("firstName"));
        
        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("lastName"));
        
 
        TableColumn emailCol = new TableColumn("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("email"));
        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
    	
    	return table;
    }
    
    
    
    public HBox addHBox() {
        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);
        hbox.setPrefWidth(900);
        hbox.setStyle("-fx-background-color: #336699;");

        Button buttonCurrent = new Button("Current");
        buttonCurrent.setPrefSize(100, 20);

        Button buttonProjected = new Button("Projected");
        buttonProjected.setPrefSize(100, 20);
        hbox.getChildren().addAll(buttonCurrent, buttonProjected);

        return hbox;
    }
    private void addStackPane(HBox hb) {

        StackPane stack = new StackPane();
        //
        
        Text helpText = new Text("?");
        helpText.setFont(Font.font("Verdana", FontWeight.BOLD, 18));
        helpText.setFill(Color.WHITE);
        helpText.setStroke(Color.web("#7080A0")); 
        
        stack.getChildren().addAll( helpText);
        stack.setAlignment(Pos.CENTER_RIGHT);
        // Add offset to right for question mark to compensate for RIGHT 
        // alignment of all nodes
        StackPane.setMargin(helpText, new Insets(0, 10, 0, 0));
        
        hb.getChildren().add(stack);
        HBox.setHgrow(stack, Priority.ALWAYS);
                
    }
    
    private ComboBox<CategoryTo> getCatList(){
    	dao = new DashBoardDao();
    	ObservableList<CategoryTo> data =
                FXCollections.observableArrayList(dao.getAllProductCat());
                
    	ComboBox<CategoryTo> combo = new ComboBox<>(data);
		return combo;
    	
    }
    
}
